<?php
class admin_knowledge extends Admin_Controller
{
	public $menu_id = 1;
	public function __construct()
	{
		parent::__construct();
		$this->load->model('knowledge/knowledge_model','knowledge');
		$this->load->model('knowledge/knowledgefiles_model','knowledgefiles');		
		$this->load->model('filetype/filetype_model','filetype');		
		$this->load->model('knowledgeauthor/knowledgeauthor_model','knowledgeauthor');
		$this->load->model('knowledgetag/knowledgetag_model','knowledgetag');
		$this->load->model('filetype/filetype_model','filetype');
	}
	
	public function index()
	{
		//
		$menu_id = $this->menu_id;
		$data['menu_id'] = $menu_id;		
		$menu_name = GetMenuProperty($menu_id,'title');
		$this->template->append_metadata(js_datepicker());	
		$data['urlpage']="admin_knowledge";
		$data['knowledgetype'] = $this->db->getarray("SELECT * FROM tbknowledgetype order by id ");
		$data['knowledgegroup'] = $this->db->getarray("SELECT * FROM tbknowledgegroup order by id ");
		if(is_login()){
			if(permission($menu_id, 'canview')=='')redirect('admin');	
			$condition = "1=1";
			$condition.= @$_GET['search']!='' ? " AND tbknowledge.title LIKE '%".$_GET['search']."%' ": "";
			$condition.= @$_GET['knowledgetype']!='' ? " AND tbknowledge.knowledgetypeid = ".$_GET['knowledgetype'] : "";
			$condition.= @$_GET['knowledgegroup']!='' ? " AND tbknowledge.knowledgegroupid = ".$_GET['knowledgegroup']: "";	
			$condition.= @$_GET['date1']!='' ? " AND (tbknowledge.publishdate BETWEEN '".Date2DB($_GET['date1'])."' AND '".Date2DB($_GET['date2'])."')" : "";
			$data['dataList'] = $this->knowledge->where($condition)->get();
			$data['pagination'] = $this->knowledge->pagination();

			$this->template->build('index',$data);
		}
		else{
			
			redirect('admin');	
		}
	}
	public function form($id=0){
		
		$menu_id=$this->menu_id;
		$curr_menu = $this->db->getone("SELECT title FROM admin_menu where id=".$menu_id);	
		$this->template->append_metadata(js_datepicker());				
		$data['id'] = $id;
		$data['rs'] = @$this->knowledge->get_row($id);		
		$data['rs2'] = @$this->knowledgefiles->where('pid='.$id)->get(FALSE,TRUE);		
		
			if($id>0)
		{
			$action='View';
			$description = $action.' '.$curr_menu.' : '.$data['rs']['title'];
			save_log($menu_id,$action,$description);
		}
		$this->template->build('form',$data);
	}



	public function save(){
		
		$this->db->debug=true;
		$menu_id=1;	
		$menu_name = GetMenuProperty($menu_id,'title');
		if($_POST['id']!='')
		{
			if(permission($menu_id, 'canedit')=='')redirect('admin_knowledge');
			$action='Update';
			$description = $action.' '.$menu_name.' : '.$_POST['title'];		
			save_log($menu_id,$action,$description);
		}else{
			if(permission($menu_id, 'canadd')=='')redirect('admin_knowledge');	
			$action='Add';
			$description = $action.' '.$menu_name.' : '.$_POST['title'];		
			save_log($menu_id,$action,$description);
		}	
		$_POST['createby']= login_data('name');
		$_POST['updateby']= login_data('name');
		$_POST['updatedate']=date("Y-m-d H:i:s");
		
		if(isset($_POST['publishdate']))$_POST['publishdate'] = Date2DB($_POST['publishdate']);	
		$id = $this->knowledge->save($_POST);
		
			$this->knowledgeauthor->delete('pid', $_POST['id']);
			if(!empty($_POST['author']))
			{
				$dataat =  $_POST['author'];
				
			    $author = explode(",", $dataat );
				for($i=0; $i<count($author); $i++)
					{
						if($author[$i] != ''){
							
							
								$at['author'] = trim($author[$i]);
								$at['pid'] = $id;
								$this->knowledgeauthor->save($at);
														
						}
					}
			}
			$this->knowledgetag->delete('pid', $_POST['id']);
			if(!empty($_POST['tag']))
			{
				$datatag =  $_POST['tag'];
				
			    $tag = explode(",", $datatag );
				for($i=0; $i<count($tag); $i++)
					{
						if($tag[$i] != ''){
							
								$tag['tag'] = trim($tag[$i]);
								$tag['pid'] = $id;
								$this->knowledgetag->save($tag);
													
							
						}
					}
			}
		// UploadFile
		//include('include/class.upload.php');	
		$i=1;
		$path="knowledgefiles/";
		for($nfile=1;$nfile<=$_POST['k_before'];$nfile++):
			$description = 'filedescription'.$nfile;
			$thumbnail_control_name = 'imagethumbnail'.$nfile;
			$file_control_name = 'filename'.$nfile;
			
			$data = array('filedescription' => $_POST[$description],'pid' => $id);
			if(@$_FILES[$thumbnail_control_name]['name']) $data['imagethumbnail'] = $this->knowledgefiles->upload($_FILES[$thumbnail_control_name], $path, 140, 140);			
			if(@$_FILES[$file_control_name]['name'])
			{
				//$this->db->debug = true;
				$data['filename'] = $this->knowledgefiles->upload($_FILES[$file_control_name], $path);
				$data['filetypeid'] = $this->db->getone("SELECT id FROM tbfiletype WHERE fileextension like '%".$this->knowledgefiles->handle->file_src_name_ext."%'");
				$this->knowledgefiles->save($data);
			}	
			

		endfor;
	
		set_notify('success', lang('save_data_complete'));
		redirect('admin_knowledge');
	} 
	

		
	function delete($id=FALSE)
	{
		if($id)
		{
			save_log(1,'Delete','Delete '.GetMenuProperty(1, 'title').' : '.$this->knowledge->get_one('title', $id));
			
			$this->knowledge->delete($id);
			$this->knowledgeauthor->delete('pid',$id);
			$this->knowledgetag->delete('pid',$id);
			$this->knowledgefiles->delete_file('pid', 'knowledgefiles/', 'imagethumbnail', $id);
			$this->knowledgefiles->delete_file('pid', 'knowledgefiles/', 'filename', $id);
			$this->knowledgefiles->delete('pid', $id);
			
			set_notify('success', lang('delete_data_complete'));
		}
		redirect('admin_knowledge');
	}
	
	function deletefiles($id)
	{
		if($id)
		{
			$this->knowledgefiles->delete_file($id, 'knowledgefiles/', 'imagethumbnail');
			$this->knowledgefiles->delete_file($id, 'knowledgefiles/', 'filename');
			$this->knowledgefiles->delete($id);
			set_notify('success', lang('delete_data_complete'));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}

	function counterfiles($idfile)
	{

		$this->db->Execute("UPDATE tbknowledgefiles SET downloads = downloads + 1  WHERE id = ".$idfile);
		
	    $filenames = $this->db->getone("SELECT filename FROM tbknowledgefiles WHERE id = ".$idfile);
		$this->load->helper('download');
		$data = file_get_contents("knowledgefiles/".basename( $filenames));
		$name = basename( $filenames);
		force_download($name, $data); 

	}
	
	
	
	
}
?>